package com.cloudptt.wp.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.product.NetWorkBroadcast;
import com.cloudptt.api.product.NetworkListener;
import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.adapter.GCallEventAdapter;
import com.cloudptt.api.product.adapter.ICallInfoDesc;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.group.HalfCallInfo;
import com.cloudptt.api.product.func.call.single.LaunchCallInfo;
import com.cloudptt.api.product.func.sys.SysStateInfo;
import com.cloudptt.api.product.func.sys.listener.SysListener;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.update.UpdateListener;
import com.cloudptt.wp.R;
import com.cloudptt.wp.ScreenManager;
import com.cloudptt.wp.SysApplication;
import com.cloudptt.wp.ui.listen.FloorStateListen;
import com.cloudptt.wp.ui.views.GroupcallStateLayout;
import com.cloudptt.wp.ui.views.OfflineStateLayout;
import com.mcptt.db.model.AddrInfo;

public abstract class BaseActivity extends AppCompatActivity implements NetworkListener,SysListener,ICallInfoDesc, UpdateListener {
	private GroupcallStateLayout mNewNotificationLayout;
	private OfflineStateLayout mOfflineStateLayout;

	private Product product;
	private GCallEventAdapter callEventAdapter = null;
	private FloorStateListen floorStateListen = null;
	private boolean isOnline = true;
	private String laseCallInfoDesc ="";

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	public void initNotificationLayout()
	{
		mNewNotificationLayout = (GroupcallStateLayout)findViewById(R.id.notification_new);
		mOfflineStateLayout = (OfflineStateLayout) findViewById(R.id.offline_status_layout);
		if(findViewById(R.id.backIv) != null) {
			ImageView backIv = (ImageView) findViewById(R.id.backIv);
			backIv.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					doBackup();
				}
			});
			TextView titleTv = (TextView) findViewById(R.id.titleTv);
			titleTv.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					doBackup();
				}
			});
		}
		ScreenManager.getScreenManager( ).pushActivity( this );
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.debug(this.getClass(), "base onResume");
		if(product == null){
			SysApplication app = ((SysApplication) getApplicationContext());
			this.product = app.getProduct();

		}
		if(product != null) {
			this.product.setNetworkListener(this);
			this.product.setSysListener(this.getClass().getName(),this);
			if (product != null) {
				if (callEventAdapter == null) {
					callEventAdapter = product.getGCallEventAdapter();
				}
				callEventAdapter.setCallInfoDesc(this);
			}
			isOnline = product.isConnectNetwork();
			Log.debug(this.getClass(), "doLoginAction isOnline = " + isOnline);
			updateOnlineIconState(isOnline);
		}

	}

	@Override
	protected void onPause() {
		super.onPause();
		if(product != null){
			this.product.setNetworkListener(null);
//			this.product.setSysListener(this.getClass().getName(),null);
		}
		if(callEventAdapter != null)
		{
			callEventAdapter.setCallInfoDesc(null);
			callEventAdapter = null;
		}
	}

	@Override
	public void networkChange(boolean isconnected) {
		Log.debug(this.getClass(), "base networkChange isconnected = " + isconnected);
		// TODO Auto-generated method stub
//		if(!isconnected)
//		{
//			this.runOnUiThread(new Runnable() {
//				@Override
//				public void run() {
//					// TODO Auto-generated method stub
//					setIsOnlineIconState(false,getString(R.string.offline_title));
//				}
//			});
//		}
		Log.debug(this.getClass(), "base networkChange isOnline = " + isOnline + " isconnected = " + isconnected);
		if(isOnline != isconnected)
		{
			this.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					updateOnlineIconState(isconnected);
				}
			});
		}
		this.isOnline = isconnected;
	}

	@Override
	public void onSysState(SysStateInfo stateInfo)
	{
		Log.debug(this.getClass(), "base onSysState stateInfo = " + stateInfo);
		int state = stateInfo.getState();
		int cause = stateInfo.getCause();

		if(SysStateInfo.SysState_SvrStartState == state)
		{
			if(SysStateInfo.Cause_Suc != cause)
			{
				String errStr = CauseCont.getAuthErrStr(cause, BaseActivity.this);
				CommDlg.showToast(BaseActivity.this, errStr);
				goToLogin();
			}
		}
		else if(SysStateInfo.SysState_SipAuthSucState == state
				|| SysStateInfo.SysState_SipPulishSucState  == state)
		{
			setIsOnlineIconState(true,"");
		}
		else if(SysStateInfo.SysState_SvrStopState == state)
		{
			setIsOnlineIconState(false,getString(R.string.offline_title));
		}
		else if(SysStateInfo.SysState_SvrStartState == state)
		{
			setIsOnlineIconState(false,getString(R.string.offline_logining));
		}
	}
	@Override
	public void onErrer(String str)
	{

	}
	@Override
	public void onForceDetach()
	{

	}
	@Override
	public void onKeepAlive(boolean isconnected)
	{
		Log.debug(this.getClass(), "base onKeepLive isOnline = " + isOnline + " isconnected = " + isconnected);
		if(isOnline != isconnected)
		{
			this.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					updateOnlineIconState(isconnected);
				}
			});
		}
		this.isOnline = isconnected;
	}
	@Override
	public void onSvrRestart()
	{

	}

	private void updateOnlineIconState(boolean isOnline)
	{
		if(!isOnline)
		{
//						resetPttLayout();
//						showPttLayout(false);
			setIsOnlineIconState(false,getString(R.string.offline_title));
		}
		else
		{
			setIsOnlineIconState(true,"");
		}
	}

	private void setIsOnlineIconState(boolean isOnline,String offlineDesc){
		Log.debug(this.getClass(),"base setIsOnlineIconState isOnline = " + isOnline);
		if(isOnline){
			mOfflineStateLayout.setVisibility(View.GONE);
		}else{
			mOfflineStateLayout.setVisibility(View.VISIBLE);
			mOfflineStateLayout.setOfflinDesc(offlineDesc);
			//			mOnlineStateLayout.setVisibility(View.GONE);
		}
//		if(mapFragment != null) {
//			mapFragment.setStatusOnLine(isOnline);
//		}
	}
	protected void goToLogin()
	{
		Intent intent = new Intent();
		intent.setClass(BaseActivity.this, Login.class);
		//		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
	}
	@Override
	public void updateCallUser(String callKey,boolean isShow,String callInfoDesc)
	{
		Log.debug(this.getClass(), "base updateCallUser callKey = " + callKey + " isShow = " + isShow + " callInfoDesc = " + callInfoDesc);
		this.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if(isShow)
				{
					mNewNotificationLayout.setVisibility(View.VISIBLE);
				}
				else
				{
					mNewNotificationLayout.setVisibility(View.GONE);
					mNewNotificationLayout.onCallEnd(callKey);
				}
				mNewNotificationLayout.setGroupName(callInfoDesc);
			}
		});
	}
	@Override
	public void updateCallInfoDesc(String callKey,int callState,String callInfoDesc)
	{
		Log.debug(this.getClass(), "base updateCallInfoDesc callKey = " + callKey + " callState = " + callState + " callInfoDesc = " + callInfoDesc);
		this.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				mNewNotificationLayout.setState(callInfoDesc);
				if(callState == CallConst.CallState_Activite)
				{
					HalfCallInfo info = callEventAdapter.getHalfCallInfo(callKey);
					CallAttr callAttr = info.getCallAttr();
					AddrInfo peerInfo = callAttr.getGroupInfo();
					LaunchCallInfo launchCallnfo = callAttr.toLaunchCallInfo(false,callState,CallConst.CallMediaState_None,true);

					mNewNotificationLayout.setCallInfo(peerInfo,launchCallnfo);
				}
				if(floorStateListen != null)
				{
					floorStateListen.onCallState(callKey,callState);
				}
			}
		});
	}
	@Override
	public void updateFloorInfoDesc(String callKey,int speakState,String callInfoDesc)
	{
		Log.debug(this.getClass(), "base updateFloorInfoDesc callKey = " + callKey + " speakState = " + speakState + " callInfoDesc = " + callInfoDesc);
		this.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				laseCallInfoDesc = callInfoDesc;
				mNewNotificationLayout.setState(callInfoDesc);
			}
		});
	}
	@Override
	public void updateFloorState(String callKey,int floorState,boolean isIdle)
	{
		Log.debug(this.getClass(), "base updateFloorState callKey = " + callKey + " floorState = " + floorState + " isIdle = " + isIdle);
		this.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if(floorState == CallConst.FloorState_Permission)
				{
					mNewNotificationLayout.updateHasVoice(true);
				}
				else if(floorState == CallConst.FloorState_NoPermission)
				{
					if(!isIdle)
					{
						mNewNotificationLayout.updateHasVoice(true);
					}
					else
					{
						mNewNotificationLayout.updateHasVoice(false);
					}
				}
				else
				{
					mNewNotificationLayout.updateHasVoice(false);
				}
				if(floorStateListen != null)
				{
					floorStateListen.onFloorState(callKey,floorState,isIdle);
				}
			}
		});
	}
	@Override
	public void updateSecDesc(String callKey,String secStr)
	{
		Log.debug(this.getClass(), "base updateSecDesc callKey = " + callKey + " secStr = " + secStr);
		this.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				mNewNotificationLayout.updateTimer(secStr);
				mNewNotificationLayout.setState(laseCallInfoDesc);
			}
		});

	}

	//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
//        	ScreenManager.getScreenManager().popActivity();
//            return true;
//        } else
//            return super.onKeyDown(keyCode, event);
//    }

	@Override
	public void onUpdate(int state) {
		// TODO Auto-generated method stub
	}

	public void setFloorStateListen(FloorStateListen floorStateListen) {
		this.floorStateListen = floorStateListen;
	}
	protected void doBackup()
	{
		Log.debug(this.getClass(), "doBackup");
		ScreenManager.getScreenManager().popAllActivityExceptOne(this,MainActivity.class);
		overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
	}
	@Override
	public void finish() {
		super.finish( );
	}
}
